import 'package:flutter/material.dart';
import 'package:tiktokadult/alert_business/video/video_line_menu_alert.dart';
import 'package:tiktokadult/assets_tool/images.dart';
import 'package:tiktokadult/config/address.dart';
import 'package:tiktokadult/hj_model/video_model.dart';
import 'package:tiktokadult/hj_utils/screen.dart';
import 'package:tiktokadult/hj_utils/widget_utils.dart';
import 'package:tiktokadult/tools_base/debug_log.dart';
import '../../../hj_model/splash/domain_source_model.dart';

class VideoTabbarMenuWidget extends StatefulWidget {
  final PageController pageController;
  final VideoModel? model;

  const VideoTabbarMenuWidget(
    this.pageController, {
    this.model,
    super.key,
  });

  @override
  State<StatefulWidget> createState() {
    return _VideoTabbarMenuWidgetState();
  }
}

class _VideoTabbarMenuWidgetState extends State<VideoTabbarMenuWidget> {
  late int selectIndex = pageController.initialPage;

  PageController get pageController => widget.pageController;

  VideoModel? get videoModel => widget.model;

  bool get isCartoon {
    return videoModel?.videoType == 1;
  }

  int get commentPageIndex {
    if (isCartoon == true) {
      return 1;
    }
    return 1;
  }

  @override
  void initState() {
    super.initState();

    pageController.addListener(_listenCallback);
  }

  ///获取评论列表
  void _listenCallback() {
    int currentIndex = 0;
    if (pageController.offset < screen.screenWidth - 50) {
      currentIndex = 0;
    } else {
      currentIndex = 1;
    }
    if (selectIndex != currentIndex) {
      selectIndex = currentIndex;
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    Domain? selectedCnd;
    try {
      selectedCnd = Address.cdnAddressLists.firstWhere((element) => element.url == Address.cdnAddress);
    } catch (e) {
      debugLog(e);
    }
    return Container(
      height: 40,
      child: Column(
        children: [
          Expanded(
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.end,
              children: [
                12.sizeBoxW,
                _buildDetailMenu(0, "简介"),
                _buildCommentCountMenu(),
                const Spacer(),
                Container(
                  alignment: Alignment.centerRight,
                  child: InkWell(
                    onTap: () async {
                      await VideoLineMenuAlert.show();
                      setState(() {});
                    },
                    child: Row(
                      children: [
                        Image.asset("line_switch.webp".videoPath, width: 16),
                        2.sizeBoxW,
                        Text(
                          "${selectedCnd?.desc}",
                          style: TextStyle(
                            color: Color(0xff989898),
                            fontSize: 12,
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
                12.sizeBoxW,
              ],
            ),
          ),
          Container(
            height: 0.5,
            color: Colors.white.withOpacity(0.1),
          ),
        ],
      ),
    );
  }

  Widget _buildDetailMenu(int index, String title) {
    bool isSelected = selectIndex == index;
    return InkWell(
      onTap: () {
        pageController.jumpToPage(index);
        selectIndex = index;
        setState(() {});
      },
      child: Container(
        padding: EdgeInsets.only(right: 12),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              title,
              style: TextStyle(
                color: isSelected ? Color(0xE5FFFFFF) : Color(0x8CFFFFFF),
                fontSize: isSelected ? 18: 16,
                fontWeight: isSelected ? FontWeight.w600 : FontWeight.w400,
              ),
            ),
            2.sizeBoxH,
            Container(
              width: 16,
              height: 0,
              decoration: BoxDecoration(
                color: isSelected ? Colors.transparent : Colors.transparent,
                borderRadius: BorderRadius.vertical(top: Radius.circular(3)),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildCommentCountMenu() {
    bool isSelected = selectIndex == commentPageIndex;
    return InkWell(
      onTap: () {
        pageController.jumpToPage(commentPageIndex);
        selectIndex = commentPageIndex;
        setState(() {});
      },
      child: Container(
        padding: EdgeInsets.only(left: 12),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              "评论",
              style: TextStyle(
                color: isSelected ? Color(0xE5FFFFFF) : Color(0x8CFFFFFF),
                fontSize: isSelected ? 18: 16,
                fontWeight: isSelected ? FontWeight.w600 : FontWeight.w400,
              ),
            ),
            2.sizeBoxH,
            Container(
              width: 16,
              height: 0,
              decoration: BoxDecoration(
                color: isSelected ? Colors.transparent: Colors.transparent,
                borderRadius: BorderRadius.vertical(top: Radius.circular(3)),
              ),
            ),
          ],
        ),
      ),
    );
  }

  @override
  void dispose() {
    pageController.removeListener(_listenCallback);
    super.dispose();
  }
}
